﻿using Google.Protobuf.WellKnownTypes;
using Mobileterminal.Common;
using Mobileterminal.Dtos;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mobileterminal.Forms
{
    public partial class ResourcestatisForm : UIPage
    {
        public static List<ApparaData> ApparatusData = new List<ApparaData>();
        private List<DepartmentInfo> departmentInfos = new List<DepartmentInfo>();
        private List<PackageInfo> packageInfos = new List<PackageInfo>();

        // 顶部统计数据
        private int totalStaff = 129;
        private int totalPackages = 380;
        private int totalEquipment = 31;

        // 图表控件
        private UIBarChart packageBarChart;
        private UIBarChart equipmentChart;

        public ResourcestatisForm()
        {
            InitializeComponent();
            // 注册初始化事件
            this.Initialize += ResourcestatisForm_Initialize;
        }

        private void ResourcestatisForm_Initialize(object sender, EventArgs e)
        {
            // 加载数据
            LoadData();

            // 创建主布局
            CreateMainLayout();
        }

        private void LoadData()
        {
            // 示例数据 - 器械数据
            if (ApparatusData.Count == 0)
            {

                ApparatusData.Add(new ApparaData { Name = "手术钳", Count = 5 });
                ApparatusData.Add(new ApparaData { Name = "手术刀", Count = 5 });
                ApparatusData.Add(new ApparaData { Name = "手术剪", Count = 2 });
                ApparatusData.Add(new ApparaData { Name = "持针器", Count = 4 });
                ApparatusData.Add(new ApparaData { Name = "拉钩", Count = 6 });
                ApparatusData.Add(new ApparaData { Name = "清洗机", Count = 2 });
                ApparatusData.Add(new ApparaData { Name = "消毒机", Count = 3 });

            }

            // 示例数据 - 器械包数据
            packageInfos.Clear();
            packageInfos.Add(new PackageInfo { Name = "手术包", Count = 4 });
            packageInfos.Add(new PackageInfo { Name = "手术刀", Count = 3 });
            packageInfos.Add(new PackageInfo { Name = "心胸手术包", Count = 5 });
            packageInfos.Add(new PackageInfo { Name = "神经外科包", Count = 3 });

            // 示例数据 - 科室信息
            departmentInfos.Clear();
            departmentInfos.Add(new DepartmentInfo { Name = "手术室", Count = 9 });
            departmentInfos.Add(new DepartmentInfo { Name = "外科", Count = 2 });
            departmentInfos.Add(new DepartmentInfo { Name = "内科", Count = 3 });
            departmentInfos.Add(new DepartmentInfo { Name = "骨科", Count = 1 });
            departmentInfos.Add(new DepartmentInfo { Name = "儿科", Count = 1 });
            // departmentInfos.Add(new DepartmentInfo { Name = "整形外科", Count = 20 });
            // departmentInfos.Add(new DepartmentInfo { Name = "胸外科", Count = 15 });
            // departmentInfos.Add(new DepartmentInfo { Name = "皮肤科", Count = 10 });
            // departmentInfos.Add(new DepartmentInfo { Name = "传染科", Count = 10 });
        }

        private void CreateMainLayout()
        {
            // 清除现有控件
            this.Controls.Clear();

            // 创建主TableLayoutPanel
            TableLayoutPanel mainLayout = new TableLayoutPanel();
            mainLayout.Dock = DockStyle.Fill;
            mainLayout.ColumnCount = 1;
            mainLayout.RowCount = 2;
            mainLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
            mainLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 110F)); // 顶部统计面板
            mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100F)); // 图表区域
            this.Controls.Add(mainLayout);

            // 创建顶部统计面板
            UIPanel topPanel = new UIPanel();
            topPanel.Dock = DockStyle.Fill;
            topPanel.Text = "资源分析";
            topPanel.Font = new Font("微软雅黑", 12, FontStyle.Bold);
            mainLayout.Controls.Add(topPanel, 0, 0);

            // 创建图表区域面板
            TableLayoutPanel chartsLayout = new TableLayoutPanel();
            chartsLayout.Dock = DockStyle.Fill;
            chartsLayout.ColumnCount = 2;
            chartsLayout.RowCount = 2;
            chartsLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
            chartsLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
            chartsLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
            chartsLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
            chartsLayout.Padding = new Padding(10);
            mainLayout.Controls.Add(chartsLayout, 0, 1);

            // 初始化顶部统计信息
            InitStatisticInfo(topPanel);

            // 初始化图表并添加到布局
            InitBarChart();
            uiBarChart1.Dock = DockStyle.Fill;
            chartsLayout.Controls.Add(uiBarChart1, 0, 0);

            InitPieChart();
            uiPieChart1.Dock = DockStyle.Fill;
            chartsLayout.Controls.Add(uiPieChart1, 0, 1);

            // 创建设备信息图表
            equipmentChart = new UIBarChart();
            equipmentChart.Dock = DockStyle.Fill;
            chartsLayout.Controls.Add(equipmentChart, 1, 0);
            InitEquipmentChart();

            // 创建器械包图表
            packageBarChart = new UIBarChart();
            packageBarChart.Dock = DockStyle.Fill;
            chartsLayout.Controls.Add(packageBarChart, 1, 1);
            InitPackageBarChart();
        }

        private void InitStatisticInfo(UIPanel topPanel)
        {

            // 清空顶部面板内容
            topPanel.Controls.Clear();

            // 创建一个空白面板
            Panel emptyPanel = new Panel();
            emptyPanel.Dock = DockStyle.Fill;
            emptyPanel.BackColor = Color.Transparent;
            topPanel.Controls.Add(emptyPanel);
        }

        private void InitBarChart()
        {
            // 确保控件存在
            if (uiBarChart1 == null) return;

            // 清除现有数据
            uiBarChart1.Option.Series.Clear();  // 检查是否为null
            uiBarChart1.Option.XAxis.Data.Clear();// 检查是否为null

            // 设置标题
            uiBarChart1.Option.Title = new UITitle();
            uiBarChart1.Option.Title.Text = "器械信息";
            uiBarChart1.Option.Title.SubText = "";

            // 设置X轴标签垂直显示
            uiBarChart1.Option.XAxis.AxisLabel.Angle = 90; // 使用Angle设置旋转角度

            // 设置图表内边距，为垂直标签留出更多空间
            if (uiBarChart1.Option.Grid == null)
                // uiBarChart1.Option.Grid = new UIGrid();// 检查是否为null
                uiBarChart1.Option.Grid.Left = 80; // 增加左侧边距
            uiBarChart1.Option.Grid.Bottom = 80; // 增加底部边距

            // 设置图例
            uiBarChart1.Option.Legend = new UILegend();
            uiBarChart1.Option.Legend.Orient = UIOrient.Vertical; // 改为垂直排列
            uiBarChart1.Option.Legend.Top = UITopAlignment.Top;
            uiBarChart1.Option.Legend.Left = UILeftAlignment.Left;
            uiBarChart1.Option.Legend.AddData("器械数量");

            // 设置工具提示
            uiBarChart1.Option.ToolTip = new UIBarToolTip();

            // 添加X轴数据
            foreach (var item in ApparatusData)
                uiBarChart1.Option.XAxis.Data.Add(item.Name);

            // 添加器械数据系列
            UIBarSeries apparatusSeries = new UIBarSeries();
            apparatusSeries.Name = "器械数量";
            foreach (var item in ApparatusData)
            {
                apparatusSeries.AddData(item.Count);
            }

            uiBarChart1.Option.Series.Add(apparatusSeries);

            // 刷新图表
            uiBarChart1.Refresh();
        }

        private void InitPackageBarChart()
        {
            // 初始化X轴配置
            packageBarChart.Option.XAxis.Clear();

            // 设置标题
            packageBarChart.Option.Title = new UITitle();
            packageBarChart.Option.Title.Text = "器械包信息";

            // 设置X轴标签垂直显示
            packageBarChart.Option.XAxis.AxisLabel.Angle = 90; // 使用Angle设置旋转角度

            // 设置图表内边距
            if (packageBarChart.Option.Grid == null)
                //    packageBarChart.Option.Grid = new Sunny.UI.UIGrid();
                packageBarChart.Option.Grid.Left = 80;
            packageBarChart.Option.Grid.Bottom = 80;

            // 设置图例
            packageBarChart.Option.Legend = new UILegend();
            packageBarChart.Option.Legend.Orient = UIOrient.Vertical;
            packageBarChart.Option.Legend.Top = UITopAlignment.Top;
            packageBarChart.Option.Legend.Left = UILeftAlignment.Left;
            packageBarChart.Option.Legend.AddData("器械包数量");

            // 设置工具提示
            packageBarChart.Option.ToolTip = new UIBarToolTip();

            // 添加X轴数据
            foreach (var item in packageInfos)
                packageBarChart.Option.XAxis.Data.Add(item.Name);

            // 添加器械包数据系列
            UIBarSeries packageSeries = new UIBarSeries();
            packageSeries.Name = "器械包数量";
            foreach (var item in packageInfos)
            {
                packageSeries.AddData(item.Count);
            }

            packageBarChart.Option.Series.Add(packageSeries);

            // 刷新图表
            packageBarChart.Refresh();
        }

        private void InitPieChart()
        {
            // 确保控件存在
            if (uiPieChart1 == null) return;

            // 清除现有数据
            uiPieChart1.Option.Series.Clear();

            // 检查 Legend 是否为 null，如果是则创建
            if (uiPieChart1.Option.Legend == null)
            {
                uiPieChart1.Option.Legend = new UILegend();
            }
            else
            {
                uiPieChart1.Option.Legend.Data.Clear();
            }

            // 设置标题
            uiPieChart1.Option.Title = new UITitle();
            uiPieChart1.Option.Title.Text = "科室信息";
            uiPieChart1.Option.Title.SubText = "各科室占比";

            // 设置图例
            uiPieChart1.Option.Legend.Orient = UIOrient.Vertical;
            uiPieChart1.Option.Legend.Top = UITopAlignment.Top;
            uiPieChart1.Option.Legend.Left = UILeftAlignment.Left;

            foreach (var dept in departmentInfos)
                uiPieChart1.Option.Legend.AddData(dept.Name);

            // 设置工具提示   
            uiPieChart1.Option.ToolTip = new UIPieToolTip();

            // 添加饼图系列   
            UIPieSeries series = new UIPieSeries();
            series.Name = "科室占比";
            series.Center = new UICenter(50, 55);
            series.Radius = 70;

            // 添加数据
            foreach (var dept in departmentInfos)
            {
                series.AddData(dept.Name, dept.Count);
            }

            uiPieChart1.Option.Series.Add(series);

            // 刷新图表
            uiPieChart1.Refresh();
        }

        private void InitEquipmentChart()
        {
            // 初始化选项
            equipmentChart.Option.XAxis.Clear(); // 清除现有X轴数据

            // 设置标题
            equipmentChart.Option.Title = new UITitle();
            equipmentChart.Option.Title.Text = "设备信息";

            // 设置图表内边距
            if (equipmentChart.Option.Grid == null)
                //  equipmentChart.Option.Grid = new UIGrid();
                equipmentChart.Option.Grid.Left = 80;
            equipmentChart.Option.Grid.Bottom = 40;

            // 设置图例
            equipmentChart.Option.Legend = new UILegend();
            equipmentChart.Option.Legend.Orient = UIOrient.Horizontal;
            equipmentChart.Option.Legend.Top = UITopAlignment.Top;
            equipmentChart.Option.Legend.Left = UILeftAlignment.Left;

            // 添加X轴数据
            equipmentChart.Option.XAxis.Data.Add("消毒设备");
            equipmentChart.Option.XAxis.Data.Add("清洗设备");
            equipmentChart.Option.XAxis.Data.Add("打包设备");
            equipmentChart.Option.XAxis.Data.Add("灭菌设备");

            // 添加设备数据系列
            UIBarSeries equipmentSeries = new UIBarSeries();
            equipmentSeries.Name = "设备数量";
            equipmentSeries.AddData(5);
            equipmentSeries.AddData(4.5);
            equipmentSeries.AddData(4.2);
            equipmentSeries.AddData(3.8);

            equipmentChart.Option.Series.Add(equipmentSeries);

            // 刷新图表
            equipmentChart.Refresh();
        }

        private void uiBarChart1_Click(object sender, EventArgs e)
        {
            // 可以添加点击事件处理，例如显示详细信息
        }

        private void uiPieChart1_Click(object sender, EventArgs e)
        {
            // 可以添加点击事件处理，例如显示详细信息
        }
    }

    // 数据模型类
    public class DepartmentInfo
    {
        public string Name { get; set; }
        public int Count { get; set; }
    }

    public class PackageInfo
    {
        public string Name { get; set; }
        public int Count { get; set; }
    }

    public class ApparaData
    {
        public string Name { get; set; }
        public string ApparatusName { get { return Name; } set { Name = value; } }
        public int Count { get; set; }
    }
}
